package cn.zl.springbootmqtt.pub;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @Description:MQTT推送回调类
 * @Author: zhanglang
 * @Date: 2020/10/27 14:40
 */
public class PushCallBack implements MqttCallback {

    /**
     * 连接丢失，用于重连
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("连接已断开，尝试重新连接。。。");
    }

    /**
     * 消息已经送达，接收到消息
     * @param topic
     * @param mqttMessage
     * @throws Exception
     */
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        // subscribe后得到的消息会执行到这里面
        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + mqttMessage.getQos());
        System.out.println("接收消息内容 : " + new String(mqttMessage.getPayload()));
    }

    /**
     * publish后会执行到这里，发送的状态
     * @param iMqttDeliveryToken
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        System.out.println("deliveryComplete..." + iMqttDeliveryToken.isComplete());
    }
}
